package com.myadapter;

import java.util.ArrayList;

import com.bean.Defaults;
import com.bean.News;
import com.bean.TopicColumns;
import com.bean.TopicFeed;
import com.bean.paramz;
import com.example.lizhiinformation.R;
import com.hb.views.PinnedSectionListView.PinnedSectionListAdapter;
import com.myadapter.FrontPageadapter.ViewHolder;
import com.tools.ImageLoader;
import com.tools.Path_URL;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 专题的详细信息
 * 
 * @author Administrator
 * 
 */
public class TopicAdapter extends BaseAdapter implements
		PinnedSectionListAdapter {
	private Context context;
	private paramz paramz;
	private View view;
	private LayoutInflater inflater;
	ArrayList<Object> list = new ArrayList<Object>();

	public TopicAdapter(Context context, com.bean.paramz paramz, View view) {
		super();
		this.context = context;
		this.paramz = paramz;
		this.view = view;
		inflater = LayoutInflater.from(context);
		list.add("头条");
		for (Defaults defaulobj : paramz.getDefaults()) {
			list.add(defaulobj);
		}
		for (TopicColumns columns : paramz.getColumns()) {

			list.add(columns.getName());
			for (TopicFeed feeds : columns.getFeeds()) {
				list.add(feeds);
			}
		}
	}

	@Override
	public int getCount() {

		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public String getItem(int arg0) {
		// TODO Auto-generated method stub
		if (list.get(arg0) instanceof Defaults) {
			Defaults defaults = (Defaults) list.get(arg0);
			return defaults.getOid();
		} else {
			if(list.get(arg0) instanceof TopicFeed){
				TopicFeed feeds = (TopicFeed) list.get(arg0);
				return feeds.getOid();
			}
			
		}
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 2;
	}

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		if (list.get(position) instanceof String) {
			return 1;
		}
		return 0;
	}

	@SuppressWarnings("null")
	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		switch (getItemViewType(position)) {
		case 0:

			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = inflater.inflate(
						R.layout.fromtpage_list_category, null);
				holder = new ViewHolder();
				holder.textview1 = (TextView) convertView
						.findViewById(R.id.fromentpage_list_category_textView1);
				holder.textview2 = (TextView) convertView
						.findViewById(R.id.fromentpage_list_category_textView2);
				holder.imagetView = (ImageView) convertView
						.findViewById(R.id.fromentpage_list_category_imageview1);
				holder.title=(TextView) convertView.findViewById(R.id.title);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();

			}
			if (list.get(position) instanceof TopicFeed) {
				TopicFeed feed = (TopicFeed) list.get(position);
				holder.textview1.setText(feed.getData().getSubject());
				holder.textview2.setText(feed.getData().getSummary());
				new ImageLoader();	
				ImageLoader.cachr(context, Path_URL.IMAGEURL
						+ feed.getData().getCover(), holder.imagetView, view);
			} else {
				Defaults defaults = (Defaults) list.get(position);
				holder.textview1.setText(defaults.getData().getSubject());
				holder.textview2.setText(defaults.getData().getSummary());
				new ImageLoader();
				ImageLoader.cachr(context, Path_URL.IMAGEURL
						+ defaults.getData().getCover(), holder.imagetView,
						view);
			}

			break;
		case 1:
			ViewHolder1 holder1 = null;
			if (convertView == null) {
				holder1 = new ViewHolder1();
				convertView = inflater.inflate(R.layout.topc_name, null);
				holder1.nametextview = (TextView) convertView
						.findViewById(R.id.topcname);
				convertView.setTag(holder1);
			} else {
				holder1 = (ViewHolder1) convertView.getTag();
			}
			String name = (String) list.get(position);
			holder1.nametextview.setText(name);
			break;
		default:
			break;
		}
		return convertView;
	}

	class ViewHolder {
		private TextView textview1;
		private TextView textview2;
		private ImageView imagetView;
		private TextView title;

	}

	class ViewHolder1 {
		private TextView nametextview;
	}

	@Override
	public boolean isItemViewTypePinned(int viewType) {
		if (viewType == 1) {
			return true;
		}

		return false;
	}
}
